package main.java.com.async.app;

import java.io.IOException;
import java.net.URL;
import java.util.Optional;

import com.ptc.cipjava.jxthrowable;
import javafx.concurrent.Task;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import main.component.infodialog.InfoDialogControl;
import main.java.com.async.middleware.StageManager;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

public class MainDialog extends Application {

	public static Stage _primary_stage;
	public static Stage _setting_stage;

	private static final String ROOT_PATH = System.getProperty("user.dir") + "\\";
	private static final String CONFIG_FILE_PATH = "./setting/config.json";

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws IOException {
//		StageManager用于跨窗口传值
		StageManager._var.put("root_path", ROOT_PATH);
		StageManager._var.put("config_file_path", CONFIG_FILE_PATH);

		String fxmlFilePath = "/main/resources/views/ModelLibrary.fxml";
		String cssFilePath = "/main/resources/css/ModelLibrary.css";
		AnchorPane root = loadFxmlFile(fxmlFilePath);
		Scene scene = new Scene(root);
		loadCssFile(scene, cssFilePath);

		primaryStage.setScene(scene);
		primaryStage.setResizable(false);
		primaryStage.setTitle("模型库");
		primaryStage.getIcons().add(new Image("/main/resources/images/library_icon.png"));
		primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
			public void handle(WindowEvent event) {
				InfoDialogControl infoDialogControl = new InfoDialogControl(Alert.AlertType.CONFIRMATION, "关闭?", "是否关闭本程序?", primaryStage);
				Optional<ButtonType> result = infoDialogControl.showAndWait();
				if(result.isPresent() && result.get() == ButtonType.OK){
//			    	关闭窗口是触发系统垃圾回收
					System.gc();
					if(StageManager._connection.get("connection") != null){
						try {
							StageManager._connection.get("connection").Disconnect(1000);
						} catch (jxthrowable x) {
							x.printStackTrace();
						}
					}
					System.exit(0);
				}
				else {
					event.consume();
				}
			}});
		primaryStage.show();
		_primary_stage = primaryStage;
		StageManager._stage.put("mainStage", primaryStage);
	}

	/**
	 * 加载Fxml文件
	 *
	 * @param fxmlFilePath 文件路径
	 * @return 返回AnchorPane类对象
	 * @throws IOException
	 */
	private AnchorPane loadFxmlFile(String fxmlFilePath) throws IOException {
		FXMLLoader fxmlLoader = new FXMLLoader();
		URL fxmlUrl = getClass().getResource(fxmlFilePath);
		fxmlLoader.setLocation(fxmlUrl);
		return fxmlLoader.load();
	}

	/**
	 * 给scene添加CSS样式
	 * @param scene 需要添加的scene对象
	 * @param cssFilePath css文件路径
	 */
	private void loadCssFile(Scene scene, String cssFilePath) {
		URL cssUrl = getClass().getResource(cssFilePath);
		scene.getStylesheets().add(cssUrl.toExternalForm());
	}

	/**
	 * 弹出设定对话框
	 * @throws IOException
	 */
	public void popSettingDialog() throws IOException {
		String fxmlFilePath = "/main/resources/views/Setting.fxml";
		String cssFilePath = "/main/resources/css/Setting.css";
		_setting_stage = new Stage();
		AnchorPane root = loadFxmlFile(fxmlFilePath);

		Scene settingScene = new Scene(root);
		loadCssFile(settingScene, cssFilePath);
		_setting_stage.setScene(settingScene);
		_setting_stage.initStyle(StageStyle.TRANSPARENT);
		_setting_stage.setResizable(false);
		_setting_stage.setTitle("设置");
//		设定次级对话框的所属者
		_setting_stage.initOwner(_primary_stage);
		_setting_stage.initModality(Modality.WINDOW_MODAL);
		_setting_stage.getIcons().add(new Image("/main/resources/images/setting.png"));
		_setting_stage.show();
	}

	//	关闭设定窗口
	public void closeSettingDialog() {
		_setting_stage.close();
	}
}
